package net.thxjj.adminServer.util;

import com.alibaba.fastjson.JSONObject;
import com.ctg.ag.sdk.biz.AepDeviceManagementClient;
import com.ctg.ag.sdk.biz.aep_device_management.CreateDeviceRequest;
import com.github.esiqveland.okhttp3.AwsConfiguration;
import com.github.esiqveland.okhttp3.AwsSigningInterceptor;
import net.thxjj.adminServer.entity.Const;
import okhttp3.*;

import java.io.IOException;
import java.util.Random;

/**
 * @since 2017-08-18
 * @author tyro
 *
 */
public class Tools {

	/**
	 * 返回随机数
	 * 
	 * @param n 个数
	 * @return
	 */
	public static String random(int n) {
		if (n < 1 || n > 10) {
			throw new IllegalArgumentException("cannot random " + n + " bit number");
		}
		Random ran = new Random();
		if (n == 1) {
			return String.valueOf(ran.nextInt(10));
		}
		int bitField = 0;
		char[] chs = new char[n];
		for (int i = 0; i < n; i++) {
			while (true) {
				int k = ran.nextInt(10);
				if ((bitField & (1 << k)) == 0) {
					bitField |= 1 << k;
					chs[i] = (char) (k + '0');
					break;
				}
			}
		}
		return new String(chs);
	}

	/**
	 * 指定范围的随机数
	 * 
	 * @param min
	 *            最小值
	 * @param max
	 *            最大值
	 * @return
	 */
	public static int getRandomNum(int min, int max) {
		Random random = new Random();
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}

	/**
	 * 检测字符串是否不为空(null,"","null")
	 * 
	 * @param s
	 * @return 不为空则返回true，否则返回false
	 */
	public static boolean notEmpty(String s) {
		return s != null && !"".equals(s) && !"null".equals(s);
	}

	/**
	 * 检测字符串是否为空(null,"","null")
	 * 
	 * @param s
	 * @return 为空则返回true，不否则返回false
	 */
	public static boolean isEmpty(String s) {
		return s == null || "".equals(s) || "null".equals(s);
	}
	
	/**
	 * 检测是否为数字
	 * @param s
	 * @return
	 */
	public static boolean isNumber(String s) {
		try {
			Integer.parseInt(s);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 检测是否为数字
	 * @param s
	 * @return
	 */
	public static boolean isLongNumber(String s) {
		try {
			Long.parseLong(s);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 返回随机数
	 *
	 * @param length 数字+小写字母
	 * @return
	 */
	public String randomCharData(int length){
		StringBuilder sb = new StringBuilder();
		Random rand = new Random();
		Random randdata = new Random();

		int data = 0;
		for(int i=0;i<length;i++)
		{
			int index = rand.nextInt(2);
			switch (index){
				case 0:
					data = randdata.nextInt(10);
					sb.append(data);
					break;
				case 1:
					data = randdata.nextInt(26)+97;
					sb.append((char)data);
					break;
			}
		}



		return sb.toString();
	}

	
	public static void main(String[] args) {
//		String accessKey = "AKIAZJ5FZV27XOROK566";
//		String secretKey = "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";
//		String regionName = "us-west-1";
//		String serviceName = "iotdata";
//
//		AwsConfiguration cfg = new AwsConfiguration(
//				accessKey,
//				secretKey,
//				regionName,
//				serviceName
//		);
//
//		Interceptor awsInterceptor = new AwsSigningInterceptor(cfg);
//		OkHttpClient client = new OkHttpClient.Builder()
//				// NetworkInterceptor is invoked after Host header is set by OkHttpClient, so use this
//				.addNetworkInterceptor(awsInterceptor)
//				.build();
//		try {
//			//创建OkHttpClient对象
//			//创建Request
//
//			MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
//			String requestBody = "{\"state\":{\"desired\":{\"status\":\"hush\",\"config\":{\"alarm_interval\":30,\"hold_alarm_time\":60,\"alarm_reset_time\":3,\"checkin_interval\":8,\"temperature_variation_value\":5}}}}";
//
//
//
//			Request request = new Request.Builder()
//					.url("https://mqtt.stage.kidde-remotelync.com/things/5_0000007000/shadow")//访问连接
////                    .post(RequestBody.create(mediaType, requestBody))
//					.get()
//					.build();
//
//
//
//			//创建Call对象
//			Call call = client.newCall(request);
//			//通过execute()方法获得请求响应的Response对象
//			Response response = call.execute();
//			if (response.isSuccessful()) {
//				//处理网络请求的响应，处理UI需要在UI线程中处理
//				//...
//				System.out.println(response.body().string());
//			}else{
//				System.out.println(response.body().string());
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}


		AepDeviceManagementClient client = AepDeviceManagementClient.newClient()
				.appKey(Const.APPKEY).appSecret(Const.APPSECRET)
				.build();

		CreateDeviceRequest request = new CreateDeviceRequest();
		JSONObject param2 = new JSONObject();
		param2.put("autoObserver",0);
		JSONObject param = new JSONObject();
		param.put("productId",Const.PROFILE_PRODUCT_ID);
		param.put("imei","861050045547885");
		param.put("deviceName","123");
//		param.put("deviceSn","12345678");
		param.put("operator","admin");
		param.put("other",param2);
		 request.setParamMasterKey(Const.PROFILE_MASTER_APIKEY);	// single value
		 request.setBody(param.toJSONString().getBytes());	//具体格式见前面请求body说明
		try {
			System.out.println(client.CreateDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}

// more requests

		client.shutdown();

	}
}
